﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZealousTTS.Domain
{
    /// <summary>
    /// This Call Will Be Used to Contain the Extension Methods of Entity Context Class
    /// </summary>
    public static class ContextExtensions
    {
        /// <summary>
        /// Checks Whethear a Given Email is Exist in some certain tables [User, Contact, ComapnyAddress]
        /// <para>
        /// This is Applicable Only when a new Entity is Going to be created.
        /// In Update Mode this method should not be used. Otherwise it will create cirtical bug.        
        /// </para>
        /// </summary>
        /// <param name="context"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsDuplicateEmail(this ZealousTTSEntities context, String email)
        {
            if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, email, true) == 0) != null)
                return true;
            //else if (context.Contacts.FirstOrDefault(P => String.Compare(P.EmailPersonal, email, true) == 0 || String.Compare(P.EmailOffice, email, true) == 0) != null)
            //    return true;
            //else if (context.Addresses.FirstOrDefault(P => String.Compare(P.Email, email, true) == 0) != null)
            //    return true;

            return false;
        }
        /// <summary>
        /// Checks Whethear a Company Name is Duplicate or not
        /// In Update Mode Company ID should be Supplied
        /// </summary>
        /// <param name="context"></param>
        /// <param name="companyName"></param>
        /// <param name="companyID"></param>
        /// <returns></returns>
        //public static bool IsDuplicateCompanyName(this ZealousTTSEntities context, string companyName, int companyID = 0)
        //{
        //    if (companyID == 0)
        //    {
        //        if (context.Companies.FirstOrDefault(P => String.Compare(P.Name, companyName, true) == 0) != null)
        //            return true;
        //    }
        //    else
        //    {
        //        if (context.Companies.FirstOrDefault(P => P.ID != companyID && String.Compare(P.Name, companyName, true) == 0) != null)
        //            return true;
        //    }
        //    return false;
        //}
        /// <summary>
        /// Checks whethear a UserName is already taken or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userName"></param>
        /// <param name="userID">The User ID that will be excluded for duplicate checking. Pass 0 to check in all records</param>
        /// <returns></returns>
        public static bool IsDuplicateUserName(this ZealousTTSEntities context, string userName, int userID)
        {
            if (userID == 0)
            {
                if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Users.FirstOrDefault(P => P.ID != userID && String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            return false;
        }

    }
}
